package com.foreveross.fetl.queue;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.foreveross.fetl.application.IAirSpreadApplication;
import com.foreveross.fetl.common.vo.TaskInfoXmlBean;
import com.foreveross.fetl.queue.util.LogMessageUtils;
import com.foreveross.fetl.util.DateUtils;
import com.foreveross.fetl.util.StringUtils;

/**
 * 以航空公司为基准航班，对比这些航班在渠道上最低价价差比
 * @author Administrator
 *
 */
@Component
public class AirSpreadPreprocess {
private static Logger log = Logger.getLogger("CsairReport");
	
	
	@Inject IAirSpreadApplication airSpreadApplication;
	
	private int tasksNumber;//任务数 多少条rowkey
	
	private int failuresNumber;//失败数
	
	private int insertNumber; //插入条数 
	
	private long excuteStartTime;

	
	private String taskTime=null;
	
	public void  rollBackNumber(){
		tasksNumber=0;
		failuresNumber=0;
		insertNumber=0;
		excuteStartTime = System.currentTimeMillis();
		LogMessageUtils.create("深航报表", this.getClass().getSimpleName());
	}
	public void execute(){
		final Calendar c = Calendar.getInstance();
		c.add(Calendar.HOUR_OF_DAY, -1);
		rollBackNumber();
		taskTime=DateUtils.format(c.getTime(), "yyyy-MM-dd HH");
		try {
			process(c.getTime(), new Date());
		} catch (Exception e) {
			LogMessageUtils.build("深航报表", this.getClass().getSimpleName(), 3, StringUtils.getStackTrace(e), failuresNumber, tasksNumber, 
					insertNumber, taskTime, excuteStartTime);
		}
		LogMessageUtils.build("深航报表", this.getClass().getSimpleName(), 2, null, 
				failuresNumber, tasksNumber, insertNumber, taskTime, excuteStartTime);
		
	}
	
	public  static void main(String[] arg0){
		Date start = new Date();
		Calendar startTime = Calendar.getInstance();
		startTime.add(Calendar.HOUR_OF_DAY, 1);
		System.out.println(start.before(startTime.getTime()));
	}
	
	
	public void manualExecute(final String start,String time){
		this.rollBackNumber();
		try {
		List<String> timeList = Arrays.asList(time.split(";"));
		Calendar c = Calendar.getInstance();
		taskTime = start + " " +time;
		for (String s : timeList) {
			String str =start;
			str += s;
			Date startTime = DateUtils.parse(str, "yyyyMMddHH");
			c.setTime(startTime);
			c.add(Calendar.HOUR_OF_DAY, +1);
			Date endTime = c.getTime();
			this.process(startTime, endTime);
			}
		LogMessageUtils.build("深航报表", this.getClass().getSimpleName(), 2, null, 
				failuresNumber, tasksNumber, insertNumber, taskTime, excuteStartTime);
		} catch (Exception e) {
			LogMessageUtils.build("深航报表", this.getClass().getSimpleName(), 3, StringUtils.getStackTrace(e), failuresNumber, tasksNumber, 
					insertNumber, taskTime, excuteStartTime);
		}
	}
	
	public void process(final Date start, final Date end) {
		log.info("AirSpreadPreprocess start:"
				+ DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
		List<String> taskLines=TaskInfoXmlBean.getShenghangLine();
		if(taskLines==null || taskLines.size()==0){
			log.info("航线为空");
			return;
		}
		
		List<String> airCompanys = this.getAirCompanys();
		List<String> compareChanels =  this.getCompareChannels();
			for(String str : taskLines){
				String[] lines=str.split("-");
				Calendar cFlightDate = Calendar.getInstance();
				cFlightDate.setTime(start);
				int[] days ={3,4,8};//3天 7天 15天
				for(int i : days){
					cFlightDate.add(Calendar.DAY_OF_MONTH, i);
					try {
						tasksNumber++;
						insertNumber +=airSpreadApplication.createSingle(airCompanys, compareChanels,lines[0], lines[1], start,
								cFlightDate.getTime());
					}catch (Exception e) {
						failuresNumber++;
					}
					
				}
			}
	}
	
	/**
	 * 对比的航空公司
	 * @return
	 */
	private List<String> getAirCompanys(){
		List<String> airCompanys = new ArrayList<String>();
		airCompanys.add("20000"); //深航
		airCompanys.add("20005"); //南航
		return airCompanys;
	}
	
	/**
	 * 对比的渠道
	 * @return
	 */
	private List<String> getCompareChannels(){
		List<String> chanels=new ArrayList<String>();
		chanels.add("00001"); //淘宝
		chanels.add("00002"); //去哪儿
		chanels.add("00003"); //携程
		chanels.add("00004"); //艺龙
		return chanels;
	}

}
